﻿using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using News.CYT369.Com.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using News.CYT369.Com.Domains;
using News.CYT369.Com.Service;
using Microsoft.AspNetCore.Authorization;
using ExecuteResult = News.CYT369.Com.Models.ExecuteResult;
using DocumentService = News.CYT369.Com.Service.DocumentService;

namespace News.CYT369.Com.Controllers
{
    public class ArticlesController : AllowAnonymousController
    {

        private News.CYT369.Com.Domains.Identity.UserManager m_UserManager;

        public ArticlesController(News.CYT369.Com.Domains.Identity.UserManager userManager)
        {
            m_UserManager = userManager;
        }


        /// <summary>
        /// 列表页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Index(string id)
        {
            int total = 0;
            int page = 1;
            int rows = 20;

            if (!string.IsNullOrEmpty(id))
            {
                var argsJson = Base64.Base64ToJSON(id);
                page = argsJson.GetInteger("Page", 1);
            }

            var docs = DocumentService.Instance.GetDocuments(page, rows, ref total);

            ViewBag.page = page;
            ViewBag.rows = rows;
            ViewBag.total = total;
            ViewBag.total_page = total / rows + (total % rows > 0 ? 1 : 0);

            return base.View("Index", docs);
        }

        /// <summary>
        /// 展示详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult CView(string id)
        {
            var argsJson = Base64.Base64ToJSON(id);
            var doc = DocumentService.Instance.GetDocumentByID(argsJson.GetLong("DocID", 0));
            if (doc != null)
            {
                if (doc.GotoTarget)
                {
                    if (!string.IsNullOrEmpty(doc.TargetURL))
                    {
                        return Redirect(doc.TargetURL);
                    }
                }
                else
                {
                    var editor = News.CYT369.Com.DAL.RedisService.Instance.GetSysUserCache(doc.EditorUserID.ToString());
                    if (editor == null)
                    {
                        editor = m_UserManager.FindByIdAsync(doc.EditorUserID.ToString()).Result;
                        if (editor != null)
                        {
                            News.CYT369.Com.DAL.RedisService.Instance.SetSysUserCache(editor);
                        }
                    }
                    ViewBag.Editor = editor;
                }
            }
            else
            {
                return Redirect("~/Home/Index");
            }

            return View("CView", doc);
        }

        [HttpPost]
        public IActionResult Viewed([FromForm] long ID, [FromForm] string args)
        {
            var retVal = new ExecuteResult();

            if (!string.IsNullOrEmpty(args) && ID > 0)
            {
                var json = Base64.Base64ToJSON(args);
                if (json != null)
                {
                    var docId = json.GetLong("DocID");
                    if (docId.HasValue && docId == ID)
                    {
                        if (!DocumentService.Instance.UpdateDocumentReadStatus(docId.Value))
                        {
                            retVal.Msg = "error";
                        }
                    }
                }
            }

            return DefaultJson(retVal);
        }

        /// <summary>
        /// 提交评论
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public IActionResult DoComments()
        {
            var argsJson = Base64.Base64ToJSON(Request.Form.GetString("docId"));
            var docId = argsJson.GetLong("DocID", 0);
            string commentStr = Request.Form.GetString("comment");
            if (docId > 0)
            {
                if (!string.IsNullOrEmpty(commentStr))
                {
                    var user = this.UserSession.GetSessionUser();
                    if (user != null)
                    {
                        var comment = new News.CYT369.Com.Domains.DocumentComment()
                        {
                            DocID = docId,
                            Content = commentStr,
                            CommentUserID = user.Id,
                            FullName = user.FullName,
                            NickName = user.NickName,
                            AuditStatusID = 0,
                            IsShow = false,
                            ChangedTime = DateTime.Now
                        };

                        if (DocumentService.Instance.SaveDocumentComment(comment))
                        {

                        }
                    }
                }
            }

            return Content("ok");
        }

    }
}
